<?php
require 'libs/vendor/autoload.php';
define('BASE_URL', 'https://bq.pingzanwo.com/');
define('BASE_IMG_URL', 'https://bq.pingzanwo.com');
function wx_url( $url = '' ){
//	return BASE_URL.'bq/'.$url.'?t=&scene=4#wechat_redirect';
	return BASE_URL.'bq/'.$url;
}
function bq_url( $url = '' ){
	return BASE_URL.'weixin/'.$url;
}

function dump($arr) {
	echo '<pre>';
	var_dump($arr);
	echo '</pre>';
}
function debug($arr, $t = false){
	echo '<pre>';
	print_r($arr);
	echo '</pre>';
	if(!$t){
		exit;
	}
}
function page_num($t = '') {
	return config_item('my_page_num'.$t);
}
function sub_html($str, $len = 0){
	return mb_substr(cutstr_html($str), 0, $len, 'utf-8');
}
//去除html js
function cutstr_html($string,$tags=''){
	$string = preg_replace("'<script(.*?)<\/script>'is","",$string);
	return strip_tags($string,$tags);
}
function get_img($img,$width=0,$height=0){
	if($img){
		$CI = & get_instance();
		return $CI->config->item('base_url').'img/image?imgurl='.$img.'.___'.$width.'.___'.$height.'.___'.date('Ymd',time());
	}
//     return $CI->config->item('s_url').'upfile/image?imgurl='.$img.'__'.$width.'__'.$height.'__'.time();
}
function imgg($img) {
	if (stripos($img,'http') === 0){
		return $img;
	}else {
		if (stripos($img,'/') === 0){
			return BASE_IMG_URL.$img;
		}
		return BASE_IMG_URL.'/'.$img;
	}
}
function date_str($time = FALSE, $format='Y-m-d') {
	if( $time === FALSE ) {
		$time = time();
	}
	if( $time != 0 ) {
		switch ($format) {
			case 'long':
				$format='Y-m-d H:i:s';
				break;
			case 'short':
				$format='Y-m-d';
				break;
		}
		return date($format, $time);
	} else {
		return '';
	}
}
function date_daytime($t = 's', $time = 0) {
	if( $time == 0 ) {
		$time = time();
	}
	$s = date_str($time).' 00:00:00';
	if( $t == 'e' ) {
		$s = date_str($time).' 23:59:59';
	}
	return strtotime($s);
}
function get_parameter($name, $dVal = false, $method = 'post', $xss_filter = true) {
	$CI = &get_instance();
	parse_str($_SERVER['QUERY_STRING'], $_GET);
	$ret = strtolower($method) == 'post' ? $CI->input->post($name, $xss_filter) : $CI->input->get($name, $xss_filter);

	$ret = $ret !== false ? $ret : $dVal;
	return $ret;
}
function view_include($name = '', $data = array(), $isret = false) {
	if( $name != '' ) {
		$CI = &get_instance();
		$ret = $CI->load->view($name, $data, $isret);
		if( $isret ) {
			return $ret;
		}
	}
}
function page_title( $title = '' ) {
	return ($title == '' ? '' : $title.'_').'后台管理系统';
}
function get_client_ip($format=0) {
	$ret = '';
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$ret = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$ret = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$ret = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$ret = $_SERVER['REMOTE_ADDR'];
	}
	preg_match("/[\d\.]{7,15}/", $ret, $retmatches);
	$ret = $retmatches[0] ? $retmatches[0] : 'unknown';
	if($format) {
		$ips = explode('.', $ret);
		for($i=0;$i<3;$i++) {
			$ips[$i] = intval($ips[$i]);
		}
		return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
	} else {
		return $ret;
	}
}

// Get Server Info
function get_serverinfo(){
	return PHP_OS.' / PHP v'.PHP_VERSION.(@ini_get('safe_mode') ? ' Safe Mode' : NULL);
}
function get_server_software() {
	return $_SERVER['SERVER_SOFTWARE'];
}
function get_dbversion() {
	$ret = getListBySQL('SELECT VERSION()');
	return count($ret) > 0 ? $ret[0]['VERSION()'] : '';
}
function get_dbsize() {
	$dbsize = 0;
	$table_list = getListBySQL('SHOW TABLE STATUS');
	foreach ($table_list as $table) {
		$dbsize += $table['Data_length'] + $table['Index_length'];
	}
	$dbsize = $dbsize ? $dbsize : '0';
	return $dbsize;
}
function get_fileupload_info() {
	$fileupload = 0;
	if(@ini_get('file_uploads')) {
		$fileupload = ini_get('upload_max_filesize');
	}
	return $fileupload;
}
function gbk2utf($s) {
	return iconv('gbk', 'utf-8', $s);
}
function utf2gbk($s) {
	return iconv('utf-8','gbk' , $s);
}
function pagination_out($baseUrl, $total, $eachPageNum, $uri_segment = 3, $page_query_string = FALSE) {
	$config['uri_segment'] = $uri_segment;
	$config['base_url'] = $baseUrl;
	$config['total_rows'] = $total;
	$config['per_page'] = $eachPageNum;
	$config['page_query_string'] = $page_query_string;

	$config['cur_tag_open']	= '<strong>';
	$config['cur_tag_close']= '</strong>';
	$config['num_links'] = 5;

	$CI = &get_instance();
	$CI->pagination->initialize($config);
	return $CI->pagination->create_links();
}
function page_make_get($base_url, $total_num, $each_page_num, $uri_segment = 3) {
	$base_url = strpos('aaa'.$base_url, '?') === false ? $base_url.'?1=1' : '';
	foreach ($_GET as $k => $v) {
		if( substr($k, 0, 3) == 'sk_' ) {
			$base_url .= '&'.$k.'='.$v;
		}
	}

	$config['uri_segment'] = $uri_segment;
	$config['base_url'] = $base_url;
	$config['total_rows'] = $total_num;
	$config['per_page'] = $each_page_num;
	$config['page_query_string'] = true;

	$config['first_link'] = FALSE;
	$config['last_link'] = FALSE;

	$config['full_tag_open'] = '<ul class="pagination">';
	$config['full_tag_close'] = '</ul>';

	$config['prev_tag_open'] = '<li aria-label="Previous">';
	$config['prev_tag_close'] = '</li>';

	$config['next_tag_open'] = '<li aria-label="Next">';
	$config['next_tag_close'] = '</li>';

	$config['cur_tag_open']	= '<li class="active"><a href="#">';
	$config['cur_tag_close']= '</a></li>';

	$config['num_tag_open'] = '<li>';
	$config['num_tag_close'] = '</li>';
	$config['num_links'] = 8;

	$CI = &get_instance();
	$CI->pagination->initialize($config);
	return $CI->pagination->create_links();
}

function deleteHtml($str, $allhtml = '') {
	$str = trim($str);
	$str = strip_tags($str,$allhtml);
	$str = preg_replace("/&nbsp;/","",$str);
	$str = preg_replace("/\t/","",$str);
	$str = preg_replace("/\r\n/"," ",$str);
	$str = preg_replace("/\r/"," ",$str);
	$str = preg_replace("/\n/"," ",$str);
	$str = preg_replace("/( )+/"," ",$str);
	return trim($str);
}
function my_redirect( $alertMessage = '', $jumpToUrl = '', $siteUrl = true, $no_back = 1 ) {
	$out = '';
	if( $alertMessage != '' ) {
		$out = 'alert("'.$alertMessage.'");';
	}
	if( $jumpToUrl == '' ) {
		$ci = &get_instance();
		$jumpToUrl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $ci->config->item('base_url');
	} else {
		if( $siteUrl ) {
			$jumpToUrl = site_url($jumpToUrl);
		}
	}

	if( strpos($jumpToUrl, '?') === false ) {
		$jumpToUrl = $jumpToUrl.'?';
	}
	$jumpToUrl = $jumpToUrl.'&no_back='.$no_back;

	$out .= 'window.location.replace("' . $jumpToUrl . '");';

	$out = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">setTimeout(\'jr();\', 1);function jr(){'.$out.'}</script>';
	echo $out;
	exit();
}
function js_fancybox($alertMessage = '', $is_close = 0, $is_refresh = 0, $func = '') {
	$out = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	if( $alertMessage != '' ) {
		$out = '<script type="text/javascript">alert("'.$alertMessage.'");</script>';
	}
	$out .= '<script type="text/javascript">'
		.( $is_refresh == 1 ? 'window.parent.location.reload();' : '' )
		.( $is_close == 1 ? 'window.parent.$.fancybox.close();' : '' )
		.($func)
		.'</script>';
	echo $out;
	exit();
}

// method js page
function my_redirect_page($alertMessage = '', $jumpToUrl = '', $siteUrl = true, $method = 'page', $delay = 2 ) {
	$out = '';
	if( $alertMessage != '' ) {
		$out = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">alert("'.$alertMessage.'");</script>';
	}
	if( $jumpToUrl == '' ) {
		$ci = &get_instance();
		$jumpToUrl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $ci->config->item('base_url');
	} else {
		if( $siteUrl ) {
			$jumpToUrl = site_url($jumpToUrl);
		}
	}
	if( $method == 'js' ) {
		$out .= '<script type="text/javascript">window.location.href="' . $jumpToUrl . '";</script>';
		echo $out;
	} elseif ( $method == 'page') {
		$data['msg'] = $alertMessage;
		$data['url'] = $jumpToUrl;
		$data['delay'] = $delay;
		$CI = &get_instance();
		echo $CI->load->view('common_redirect', $data, TRUE);
	}
	exit();
}
function my_redirect_weixin_page( $alertMessage = '', $jumpToUrl = '', $siteUrl = true, $method = 'page', $delay = 2 ) {
	$out = '';
	if( $alertMessage != '' ) {
		$out = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">alert("'.$alertMessage.'");</script>';
	}
	if( $jumpToUrl == '' ) {
		$ci = &get_instance();
		$jumpToUrl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $ci->config->item('base_url');
	} else {
		if( $siteUrl ) {
			$jumpToUrl = site_url($jumpToUrl);
		}
	}
	if( $method == 'js' ) {
		$out .= '<script type="text/javascript">window.location.href="' . $jumpToUrl . '";</script>';
		echo $out;
	} elseif ( $method == 'page') {
		$data['msg'] = $alertMessage;
		$data['url'] = $jumpToUrl;
		$data['delay'] = $delay;
		$CI = &get_instance();
		echo $CI->load->view('common/redirect', $data, TRUE);
	}
	exit();
}
function command_echo( $s ) {
	echo $s, "\n";
}
function validation_is_domain($domain){
	return !empty($domain) && strpos($domain, '--') === false &&
	preg_match('/^([a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?\.)?[a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?(\.us|\.tv|\.org\.cn|\.org|\.net\.cn|\.net|\.mobi|\.me|\.la|\.info|\.hk|\.gov\.cn|\.edu|\.com\.cn|\.com|\.co\.jp|\.co|\.cn|\.cc|\.biz)$/i', $domain) ? true : false;
}

function show_array_value($array, $text="", $l=0) {
	if( is_object($array) ) {
		$array = (array)$array;
	}
	if (!is_array($array) && $l<=0) {
		$array=array($array);
	}
	$text="";
	while(list($key,$value) = each($array)) {
		if( is_object($value) ) {
			$value = (array)$value;
			$value['is_object'] = 'TRUE';
		}
		if( is_array($value) ) {
			$value['is_array'] = 'TRUE';
		}
		$text .= str_repeat(' ', $l*4)."".$key." : ";
		if (is_array($value)){
			$text.='
';
			$l++;
			$text.=show_array_value($value,$text,$l);
			$l--;
		} else {
			if( $key == 'CCNumber' ) {
				$value = str_repeat('H', strlen($value) - 4).substr($value, -4);
			}
			$text .= ( $value ).'
';
		}
	}
	return $text;
}

function curl_file_get_contents($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$file_contents = curl_exec($ch);
	curl_close($ch);
	return $file_contents;
}

function xlsx_export( $filename, $data ) {
	header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
	header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	header('Content-Transfer-Encoding: binary');
	header('Cache-Control: must-revalidate');
	header('Pragma: public');
	echo $data;
	exit(0);
}

function pages($date, $url){
	$start_ny = 201703;
	$ny = date('Ym',time());
	$page = '<ul class="pagination">';
	$year = substr($date, 0, 4);
	$month = substr($date, 4, 2);
	$js_m = $month;
	if( stripos($month) === 0 ){
		$js_m = substr($month, 1, 1);
	}else{

	}

	$ym = $year.$month;
	if( $ym < $ny ){
		$js_m = $js_m+1;
		if($js_m < 10){
			$js_m = '0'.$js_m;
		}
		$page = $page.'<li class="active"><a href="'.site_url($url.$year.$js_m).'">上一页</a></li>';
	}
	if( $ym > $start_ny ){
		$js_m = $js_m-1;
		if($js_m < 10){
			$js_m = '0'.$js_m;
		}
		$page = $page.'<li class="active"><a href="'.site_url($url.$year.$js_m).'">下一页</a></li>';
	}
	$page = $page.'</ul>';
//		dump($page);
	return $page;

}
function yue_day($date){
	$rq = array();
	$year = substr($date, 0, 4);
	$month = substr($date, 4, 2);
	$ny = date('Ym',time());
	if (in_array($month, array(1, 3, 5, 7, 8, '01', '03', '05', '07', '08', 10, 12))) {
		$day = 31;
	}elseif ($month == 2){
		if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
			$day = 29;
		} else {
			$day = 28;
		}
	} else {
		$day = 30;
	}
	if( $ny == $year.$month ){
		$day = date('j');
	}
	for($i = 1; $i <= $day; $i++){

		if($i < 10){
			$rq[] = $year.$month.'0'.$i;
		}else{
			$rq[] = $year.$month.$i;
		}
	}
	rsort($rq);
	return $rq;
}
function howlong( $ltime ){
	$nowtime = time();
	if( $nowtime > $ltime ){
		$gap = $nowtime - $ltime;
		if( $gap < 60 ){
			$ret = $gap.'秒前';
		} elseif ( $gap > 60 && $gap < 3600 ){
			$ret = floor($gap / 60).'分钟前';
		} elseif ( $gap > 3600 && $gap < 86400 ){
			$ret = floor($gap / 3600).'小时前';
		} elseif ( $gap > 86400 ){
			$ret = floor($gap / 86400).'天前';
		} elseif ( $gap > 864000 ){
            $ret = date_str($ltime);
        }
	} else {
		$ret = '刚刚';
	}
	return $ret;
}
//删除字符串中emoji表情
function filterEmoji($str){
    $str = preg_replace_callback(
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}
//删除空格
function trimall($str){
    $oldchar = array(" ", "　", "\t", "\n", "\r");
    $newchar = array("", "", "", "", "");
    return str_replace($oldchar, $newchar, $str);
}

//二维数组排序
function arraySequence($array, $field, $sort = 'SORT_DESC')
{
    $arrSort = array();
    foreach ($array as $uniqid => $row) {
        foreach ($row as $key => $value) {
            $arrSort[$key][$uniqid] = $value;
        }
    }
    array_multisort($arrSort[$field], constant($sort), $array);
    return $array;
}
//二维数组字段值拼成一维数组
function arrayKey($d, $name)
{
    $arr = [];
    foreach ($d as $key => $v) {
        $arr[$v['id']] = $v[$name];
    }
    return $arr;
}